home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / cmds / fsinstall / RCS / diskIO.c,v < prev    next >
Encoding:
Text File  |  1989-12-13  |  10.7 KB  |  362 lines

  1. head     1.2;
  2. branch   ;
  3. access   ;
  4. symbols  ;
  5. locks    ; strict;
  6. comment  @ * @;
  7.  
  8.  
  9. 1.2
  10. date     89.12.13.14.07.06;  author rab;  state Exp;
  11. branches ;
  12. next     1.1;
  13.  
  14. 1.1
  15. date     89.11.29.22.36.25;  author rab;  state Exp;
  16. branches ;
  17. next     ;
  18.  
  19.  
  20. desc
  21. @@
  22.  
  23.  
  24. 1.2
  25. log
  26. @Fixed problem with reading first sector.
  27. @
  28. text
  29. @/* 
  30.  * diskIO.c --
  31.  *
  32.  *    Routines to do I/O to a raw disk.
  33.  *
  34.  * Copyright (C) 1987 Regents of the University of California
  35.  * All rights reserved.
  36.  * Permission to use, copy, modify, and distribute this
  37.  * software and its documentation for any purpose and without
  38.  * fee is hereby granted, provided that the above copyright
  39.  * notice appear in all copies.  The University of California
  40.  * makes no representations about the suitability of this
  41.  * software for any purpose.  It is provided "as is" without
  42.  * express or implied warranty.
  43.  */
  44.  
  45. #ifndef lint
  46. static char rcsid[] = "$Header: /sprite/src/admin/fsinstall/RCS/diskIO.c,v 1.1 89/11/29 22:36:25 rab Exp Locker: rab $ SPRITE (Berkeley)";
  47. #endif not lint
  48.  
  49. #include "diskUtils.h"
  50. #include <stdio.h>
  51. #include <errno.h>
  52. #include <sys/file.h>
  53.  
  54.  
  55. /*
  56.  *----------------------------------------------------------------------
  57.  *
  58.  * Disk_SectorRead --
  59.  *    Read sectors from the disk file at a specified offset.  This combines
  60.  *    an Ioc_Reposition with the read.
  61.  *
  62.  * Results:
  63.  *    0 if could read the sector, -1 if could not.  If couldn't read
  64.  *    the disk then the error is stored in errno.
  65.  *
  66.  * Side effects:
  67.  *    Reposition the disk file's stream pointer and *buffer filled
  68.  *    with data from the disk.
  69.  *
  70.  *----------------------------------------------------------------------
  71.  */
  72. int
  73. Disk_SectorRead(openFileID, firstSector, numSectors, buffer)
  74.     int        openFileID;    /* Handle on raw disk */
  75.     int        firstSector;    /* First sector to read */
  76.     int        numSectors;    /* The number of sectors to read */
  77.     Address    buffer;        /* The buffer to read into */
  78. {
  79.     int amountRead;
  80.  
  81.     if (lseek(openFileID, firstSector * DEV_BYTES_PER_SECTOR, L_SET) < 0) {
  82.     perror("Disk_SectorRead: lseek failed");
  83.     return(-1);
  84.     }
  85.     amountRead = read(openFileID, buffer, DEV_BYTES_PER_SECTOR * numSectors);
  86.     if (amountRead < 0) {
  87.     perror("Disk_SectorRead: read failed");
  88.     return(-1);
  89.     } else if (amountRead != DEV_BYTES_PER_SECTOR * numSectors) {
  90.     fprintf(stderr, "Disk_SectorRead: short read, %d sectors, not %d\n",
  91.                amountRead / DEV_BYTES_PER_SECTOR, numSectors);
  92.     errno = 0;
  93.     return(-1);
  94.     }
  95.     return(0);
  96. }
  97.  
  98. /*
  99.  *----------------------------------------------------------------------
  100.  *
  101.  * Disk_SectorWrite --
  102.  *    Write sectors to the disk file at a specified offset.  This combines
  103.  *    an Ioc_Reposition with the write.
  104.  *
  105.  * Results:
  106.  *    0 if could write the disk, -1 if could not.  If couldn't read the
  107.  *    disk then the error number is stored in errno.
  108.  *
  109.  * Side effects:
  110.  *    The write.
  111.  *
  112.  *----------------------------------------------------------------------
  113.  */
  114. int
  115. Disk_SectorWrite(openFileID, firstSector, numSectors, buffer)
  116.     int        openFileID;    /* Handle on raw disk */
  117.     int        firstSector;    /* First sector to read */
  118.     int        numSectors;    /* The number of sectors to read */
  119.     Address    buffer;        /* The buffer to read into */
  120. {
  121.     int amountWritten;
  122.  
  123.     if (lseek(openFileID, firstSector * DEV_BYTES_PER_SECTOR, L_SET) == -1) {
  124.     perror("Disk_SectorWrite: lseek failed");
  125.     fprintf(stderr, "fd = %d, offset = %ld, whence= %d\n",
  126.         openFileID, firstSector * DEV_BYTES_PER_SECTOR, L_SET);
  127.     return(-1);
  128.     }
  129.     amountWritten = write(openFileID, buffer, 
  130.               DEV_BYTES_PER_SECTOR * numSectors);
  131.     if (amountWritten < 0) {
  132.     perror("Disk_SectorWrite: write failed");
  133.     fprintf(stderr, "fd = %d, buffer= 0x%08x, cnt = %d firstSector = %d\n",
  134.         openFileID, buffer, DEV_BYTES_PER_SECTOR * numSectors,
  135.         firstSector);
  136.  
  137.     return(-1);
  138.     } else if (amountWritten != DEV_BYTES_PER_SECTOR * numSectors) {
  139.     fprintf(stderr, "Disk_SectorWrite: short write, %d sectors, not %d\n",
  140.                 amountWritten / DEV_BYTES_PER_SECTOR, numSectors);
  141.     errno = 0;
  142.     return(-1);
  143.     }
  144.     return(0);
  145. }
  146.  
  147.  
  148. /*
  149.  *----------------------------------------------------------------------
  150.  *
  151.  * Disk_BlockRead --
  152.  *    Read blocks to the disk file at a specified block offset.
  153.  *    This has to use the disk geometry information to figure out
  154.  *    what disk sectors correspond to the block.
  155.  *
  156.  * Results:
  157.  *    0 if could read the block, -1 if could not.  If couldn't read the block
  158.  *    the the error is stored in errno.
  159.  *
  160.  * Side effects:
  161.  *    *buffer is filled with the data from the disk.
  162.  *
  163.  *----------------------------------------------------------------------
  164.  */
  165. int
  166. Disk_BlockRead(openFileID, headerPtr, firstBlock, numBlocks, buffer)
  167.     int            openFileID;    /* Handle on raw disk */
  168.     Fsdm_DomainHeader    *headerPtr;    /* Domain header with geometry
  169.                      * information */
  170.     int            firstBlock;    /* First block to read */
  171.     int            numBlocks;    /* The number of blocks to read */
  172.     Address        buffer;        /* The buffer to read into */
  173. {
  174.     register Fsdm_Geometry *geoPtr;
  175.     register int blockIndex;
  176.     register int cylinder;
  177.     register int rotationalSet;
  178.     register int blockNumber;
  179.     int firstSector;
  180.  
  181.     geoPtr = &headerPtr->geometry;
  182.     for (blockIndex = 0 ; blockIndex < numBlocks ; blockIndex++) {
  183.     blockNumber    = firstBlock + blockIndex;
  184.     cylinder    = blockNumber / geoPtr->blocksPerCylinder;
  185.     if (geoPtr->rotSetsPerCyl > 0) {
  186.         /*
  187.          * Original mapping scheme using rotational sets.
  188.          */
  189.         blockNumber        -= cylinder * geoPtr->blocksPerCylinder;
  190.         rotationalSet    = blockNumber / geoPtr->blocksPerRotSet;
  191.         blockNumber        -= rotationalSet * geoPtr->blocksPerRotSet;
  192.     
  193.         firstSector = geoPtr->sectorsPerTrack * geoPtr->numHeads * 
  194.              cylinder +
  195.              geoPtr->sectorsPerTrack * geoPtr->tracksPerRotSet *
  196.              rotationalSet + geoPtr->blockOffset[blockNumber];
  197.     } else if (geoPtr->rotSetsPerCyl == FSDM_SCSI_MAPPING){
  198.         /*
  199.          * New mapping for scsi devices.
  200.          */
  201.         firstSector = geoPtr->sectorsPerTrack * geoPtr->numHeads * 
  202.             cylinder +
  203.             blockNumber * DISK_SECTORS_PER_BLOCK - 
  204.             cylinder * 
  205.             geoPtr->blocksPerCylinder * DISK_SECTORS_PER_BLOCK;
  206.     } else {
  207.         return -1;
  208.     }
  209.     if (Disk_SectorRead(openFileID, firstSector,
  210.                  DISK_SECTORS_PER_BLOCK, buffer) < 0) {
  211.         return(-1);
  212.     }
  213.     buffer += FS_BLOCK_SIZE;
  214.     }
  215.     return(0);
  216. }
  217.  
  218. /*
  219.  *----------------------------------------------------------------------
  220.  *
  221.  * Disk_BadBlockRead --
  222.  *    Read 1 block a sector at a time, returning a bitmap corresponding
  223.  *    to the blocks that were read successfully.
  224.  *    This has to use the disk geometry information to figure out
  225.  *    what disk sectors correspond to the block.
  226.  *
  227.  * Results:
  228.  *    The bitmask of valid sectors is returned.
  229.  *
  230.  * Side effects:
  231.  *    None.
  232.  *
  233.  *----------------------------------------------------------------------
  234.  */
  235. int
  236. Disk_BadBlockRead(openFileID, headerPtr, blockNumber, buffer)
  237.     int openFileID;    /* Handle on raw disk */
  238.     Fsdm_DomainHeader *headerPtr;/* Domain header with geometry information */
  239.     int blockNumber;    /* Block to read */
  240.     Address buffer;    /* The buffer to read into */
  241. {
  242.     ReturnStatus status;
  243.     register Fsdm_Geometry *geoPtr;
  244.     register int sectorIndex;
  245.     register int cylinder;
  246.     register int rotationalSet;
  247.     int firstSector;
  248.     int valid = 0;        /* Assumes <= 32 sectors/block */
  249.  
  250.     geoPtr = &headerPtr->geometry;
  251.     cylinder    = blockNumber / geoPtr->blocksPerCylinder;
  252.     blockNumber    -= cylinder * geoPtr->blocksPerCylinder;
  253.     rotationalSet    = blockNumber / geoPtr->blocksPerRotSet;
  254.     blockNumber    -= rotationalSet * geoPtr->blocksPerRotSet;
  255.  
  256.     firstSector = geoPtr->sectorsPerTrack * geoPtr->numHeads * cylinder + 
  257.         geoPtr->sectorsPerTrack * geoPtr->tracksPerRotSet * rotationalSet +
  258.         geoPtr->blockOffset[blockNumber];
  259.     for (sectorIndex = 0; sectorIndex < DISK_SECTORS_PER_BLOCK; sectorIndex++) {
  260.     if (Disk_SectorRead(openFileID, firstSector + sectorIndex, 
  261.                 1, buffer) >= 0) {
  262.         valid |= (1 << sectorIndex);
  263.     }
  264.         buffer += DEV_BYTES_PER_SECTOR;
  265.     }
  266.     return(valid);
  267. }
  268.  
  269. /*
  270.  *----------------------------------------------------------------------
  271.  *
  272.  * Disk_BlockWrite --
  273.  *    Write blocks to the disk file at a specified block offset.
  274.  *    This has to use the disk geometry information to figure out
  275.  *    what disk sectors correspond to the block.
  276.  *    Write blocks individually if a hard error occurs during the write
  277.  *    of the entire block.
  278.  *
  279.  *    Note: ignores the error condition otherwise, so if two blocks
  280.  *    are to be written, everything but unwritable sectors will be written
  281.  *    and the error for the unwritable sector(s) would be returned.
  282.  *
  283.  * Results:
  284.  *    0 if could write the block, -1 if could not.  If couldn't write the
  285.  *    block then the error is stored in errno.
  286.  *
  287.  * Side effects:
  288.  *    None.
  289.  *
  290.  *----------------------------------------------------------------------
  291.  */
  292. int
  293. Disk_BlockWrite(openFileID, headerPtr, firstBlock, numBlocks, buffer)
  294.     int            openFileID;    /* Handle on raw disk */
  295.     Fsdm_DomainHeader    *headerPtr;    /* Domain header with geometry
  296.                      * information */
  297.     int            firstBlock;    /* First block to read */
  298.     int            numBlocks;    /* The number of blocks to read */
  299.     Address        buffer;        /* The buffer to read into */
  300. {
  301.     register Fsdm_Geometry *geoPtr;
  302.     register int blockIndex;    /* Loop counter */
  303.     register int cylinder;    /* Cylinder within domain */
  304.     register int rotationalSet;    /* Rotational Set within cylinder */
  305.     register int blockNumber;    /* Block number within rotational set */
  306.     int firstSector;
  307.  
  308.     geoPtr = &headerPtr->geometry;
  309.     for (blockIndex = 0 ; blockIndex < numBlocks ; blockIndex++) {
  310.     blockNumber    = firstBlock + blockIndex;
  311.     cylinder    = blockNumber / geoPtr->blocksPerCylinder;
  312.     if (geoPtr->rotSetsPerCyl > 0) {
  313.         /*
  314.          * Original mapping scheme using rotational sets.
  315.          */
  316.         blockNumber        -= cylinder * geoPtr->blocksPerCylinder;
  317.         rotationalSet    = blockNumber / geoPtr->blocksPerRotSet;
  318.         blockNumber        -= rotationalSet * geoPtr->blocksPerRotSet;
  319.     
  320.         firstSector = geoPtr->sectorsPerTrack * geoPtr->numHeads * 
  321.              cylinder +
  322.              geoPtr->sectorsPerTrack * geoPtr->tracksPerRotSet *
  323.              rotationalSet + geoPtr->blockOffset[blockNumber];
  324.     } else if (geoPtr->rotSetsPerCyl == FSDM_SCSI_MAPPING){
  325.         /*
  326.          * New mapping for scsi devices.
  327.          */
  328.         firstSector = geoPtr->sectorsPerTrack * geoPtr->numHeads * 
  329.             cylinder +
  330.             blockNumber * DISK_SECTORS_PER_BLOCK - 
  331.             cylinder * 
  332.             geoPtr->blocksPerCylinder * DISK_SECTORS_PER_BLOCK;
  333.     } else {
  334.         return -1;
  335.     }
  336.     if (Disk_SectorWrite(openFileID, firstSector,
  337.                  DISK_SECTORS_PER_BLOCK, buffer) < 0) {
  338.         return(-1);
  339.     }
  340.     buffer += FS_BLOCK_SIZE;
  341.     }
  342.     return(0);
  343. }
  344.  
  345. @
  346.  
  347.  
  348. 1.1
  349. log
  350. @Initial revision
  351. @
  352. text
  353. @d18 1
  354. a18 1
  355. static char rcsid[] = "$Header: /sprite/src/lib/c/disk/RCS/diskIO.c,v 1.6 89/10/02 23:17:57 jhh Exp $ SPRITE (Berkeley)";
  356. d95 1
  357. a95 1
  358.     if (lseek(openFileID, firstSector * DEV_BYTES_PER_SECTOR, L_SET) < 0) {
  359. d97 2
  360. d105 4
  361. @
  362.